﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAO;
using Framework.Library.Data.DAL;
using Framework.Library.Core.Utils;
using System.Collections;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils.Text;
using Framework.Library.Core.Utils.URLBuilder;

namespace Framework.Portal.Layouts.Admin.Components.BGTT.Courses
{
    public partial class PedagogueManager : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public PedagogueManager()
        {
        }

        public PedagogueManager(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var arrLetters = new ArrayList();
                for (char c = 'A'; c <= 'Z'; c++)
                {
                    arrLetters.Add(c.ToString());
                }
                arrLetters.Add("All");
                rptLetters.DataSource = arrLetters;
                rptLetters.DataBind();
                lblCommandName.Text = MENUNAME;
            }
        }

        protected void rptLetters_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lnkLetter = (LinkButton) e.Item.FindControl("lnkLetter");
                lnkLetter.Text = e.Item.DataItem.ToString();
                lnkLetter.CommandArgument = e.Item.DataItem.ToString();
            }
        }

        protected void rptLetters_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Alpha")
            {
                if (ConvertUtility.ToString(e.CommandArgument) != "All")
                {
                    LoadUser(e.CommandArgument.ToString());
                }
                else
                {
                    LoadUser("");
                }
            }
        }

        protected void dtgUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                PortalUser info = (PortalUser)e.Item.DataItem;
                ImageButton set = (ImageButton)e.Item.FindControl("btnSetPedagogue");
                ImageButton edit = (ImageButton)e.Item.FindControl("ImageButton1");
                if (info.TYPE == 0)
                    edit.Visible = false;
                else
                    set.Visible = false;
            }
        }
        protected void dtgUsers_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int userId = ConvertUtility.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "setpedagogue":
                    var urlParser = new SimpleURLParameterParser(Request.RawUrl);
                    var lstUrl = new Dictionary<string, string>();
                    lstUrl.Add("t", "1");
                    lstUrl.Add("uid", e.CommandArgument.ToString());
                    Response.Redirect(urlParser.CreateQueryString(lstUrl));
                    break;
                default:
                    break;
            }
        }

        private void LoadUser(string filter)
        {
            List<PortalUser> dtUsers = ManagerFactory.PortalUserManager.GetUsers("%" + filter + "%");
            List<PortalUser> lstUsers = new List<PortalUser>();
            var lstPeda = ManagerFactory.DAOEntity.GetList(new BGTTPedagogues());
            if (lstPeda != null)
            {
                foreach (var userInfo in dtUsers)
                {
                    if (lstPeda.FindIndex(x => x.USERID == userInfo.USERID) > -1)
                        userInfo.TYPE = 1;
                    else
                        userInfo.TYPE = 0;
                    lstUsers.Add(userInfo);
                }
            }
            else
                lstUsers = dtUsers;
            dtgUsers.DataSource = lstUsers;
            dtgUsers.DataBind();
        }
    }
}